package com.ipfsemc.ipfsemc.controller.v1;

import com.ipfsemc.ipfsemc.model.Apply;
import com.ipfsemc.ipfsemc.model.ApplyBase;
import com.ipfsemc.ipfsemc.model.ApplySpace;
import com.ipfsemc.ipfsemc.service.IApplyService;
import com.ipfsemc.ipfsemc.swagger.ApiModelPropertyIgnore;
import com.ipfsemc.ipfsemc.util.JsonData;

import com.ipfsemc.ipfsemc.util.RespList;
import com.ipfsemc.ipfsemc.util.Response;
import com.ipfsemc.ipfsemc.util.ResponseBase;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Api(value = "ApplyController", tags = "申请报备")
@RestController
@RequestMapping("apply")
public class ApplyController {
    private static final Logger logger = LoggerFactory.getLogger(ApplyController.class);
    @Autowired
    private IApplyService applyService;

    @ApiOperation(value = "提交申请", notes = "说明", httpMethod = "POST")
    @RequestMapping(value = "applyAdd", method = RequestMethod.POST)
    public ResponseBase apply(
            @ApiModelPropertyIgnore(
                    {"applyId", "applySate", "applyTime", "passTime", "outTime", "name", "phone", "applicantName", "applicantPhone", "reason"})
             @RequestBody Apply apply
    ) {
        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == apply.getUserId() || null == apply.getApplicantId()) {
                jsonData.setMessage("参数异常！");
            } else {
                if (apply.getUserId().equals(apply.getApplicantId())) {
                    jsonData.setMessage("对不起！不能报备自己！");
                } else {
                    int add = applyService.add(apply);
                    if (add == -4) {
                        jsonData.setMessage("你已经申请过了！！");

                    } else if (add == -3) {
                        jsonData.setMessage("申请失败！请稍后尝试！");
                    } else if (add == 1) {
                        jsonData.setMessage("此人已被绑定！不可报备！");

                    } else if (add == 0) {
                        jsonData.setMessage("此人已报备，过期后可重新报备！");
                    } else {
                        jsonData.setMessage("已提交！");
                        jsonData.setCode(0);
                    }
                }
            }
        } catch (Exception ex) {
            logger.error("[ApplyController][applyAdd]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }

    @ApiOperation(value = "删除申请记录", notes = "说明", httpMethod = "POST")
    @RequestMapping(value = "applyDel", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "申请ID", required = true, dataType = "Integer"),
    })
    public ResponseBase applyDel(Integer applyId) {
        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == applyId) {
                jsonData.setMessage("参数异常！");
            } else {
                applyService.del(applyId);
                jsonData.setMessage("删除成功！");
                jsonData.setCode(0);
            }

        } catch (Exception ex) {
            logger.error("[ApplyController][applyDel]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }


    @ApiOperation(value = "修改申请状态", notes = "说明", httpMethod = "POST")
    @RequestMapping(value = "updateState", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "申请ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "applySate", value = "申请状态  -2未通过  0已通过，待成交", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "reason", value = "不通过的理由", required = false, dataType = "String")
    })
    public ResponseBase updateState(Integer applyId, Integer applySate, String reason) {
        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == applyId) {
                jsonData.setMessage("参数异常！");
            } else {
                Apply apply = new Apply();
                apply.setApplySate(applySate);
                apply.setApplyId(applyId);
                apply.setReason(reason);
                int i = applyService.updateState(apply);
                if (i > 0) {
                    jsonData.setMessage("操作成功！");
                    jsonData.setCode(0);
                } else {
                    jsonData.setMessage("系统正忙！请稍后再试！");
                }
            }

        } catch (Exception ex) {
            logger.error("[ApplyController][updateState]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }

    @ApiOperation(value = "查看单个申请", notes = "说明", httpMethod = "GET")
    @RequestMapping(value = "selectApply", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "申请ID", required = true, dataType = "Integer"),
    })
    public Response<Apply> selectApply(Integer applyId) {
        Response<Apply> response = new Response<>();
        try {
            if (null == applyId) {
                response.setMessage("参数异常！");
            } else {
                Apply apply1 = applyService.selectApply(applyId);
                response.setResult(apply1);
                response.setMessage("操作成功！");
                response.setCode(0);
            }

        } catch (Exception ex) {
            logger.error("[ApplyController][selectApply]", ex);
            response.setMessage("系统错误,请稍后重试");
        }

        return response;
    }



    @ApiOperation(value = "查询所有申请报备记录", notes = "说明", httpMethod = "GET")
    @RequestMapping(value = "listApply", method = RequestMethod.GET)
    public RespList<Apply> listApply() {
        RespList jsonData = new RespList();

        try {
            List<Apply> list = applyService.list(null);

            jsonData.setResult(list);

            jsonData.setCode(0);
        } catch (Exception ex) {
            logger.error("[ApplyController][listApply]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiOperation(value = "FileCoin伙伴创客详情", notes = "说明", httpMethod = "GET")
    @RequestMapping(value = "userFilCoin", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applicantId", value = "申请人ID 即当前的用户", required = true, dataType = "String")
    })
    public RespList<ApplyBase> userFilCoin(String applicantId) {
        RespList jsonData = new RespList();
        try {
            if (null == applicantId) {
                jsonData.setMessage("参数异常！");
            } else {
                List<ApplyBase> list = applyService.selectFilCon(applicantId);

                jsonData.setResult(list);

                jsonData.setCode(0);
            }
        } catch (Exception ex) {
            logger.error("[ApplyController][userApply]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiOperation(value = "FileCoin伙伴详情", notes = "说明", httpMethod = "GET")
    @RequestMapping(value = "FileCoinDetails", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applicantId", value = "申请人ID 即当前的用户", required = true, dataType = "String")
    })
    public RespList<ApplySpace> FileCoinDetails(String applicantId) {
        RespList jsonData = new RespList();
        try {
            if (null == applicantId) {
                jsonData.setMessage("参数异常！");
            } else {
                List<ApplySpace> list = applyService.FileCoinDetails(applicantId);

                jsonData.setResult(list);
                jsonData.setMessage("成功！");
                jsonData.setCode(0);
            }
        } catch (Exception ex) {
            logger.error("[ApplyController][userApply]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }


    @ApiOperation(value = "FileCoin伙伴报备列表", notes = "说明", httpMethod = "GET")
    @RequestMapping(value = "userApply", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "申请人ID  即当前的用户", required = true, dataType = "String"),
            @ApiImplicitParam(name = "applySate", value = "申请状态  （创客详情必须为1）", required = true, dataType = "Integer")
    })
    public RespList<Apply> userApply(String applicantId, Integer applySate) {
        RespList jsonData = new RespList();
        try {
            if (null == applicantId) {
                jsonData.setMessage("参数异常！");
            } else {
                Apply apply = new Apply();
                apply.setApplicantId(applicantId);
                apply.setApplySate(applySate);

                List<Apply> list = applyService.selectByUser(apply);

                jsonData.setResult(list);

                jsonData.setCode(0);
            }
        } catch (Exception ex) {
            logger.error("[ApplyController][userApply]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiOperation(value = "是否报备", notes = "根据手机号查询用户是否报备", httpMethod = "GET")
    @RequestMapping(value = "isApply", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applicantPhone", value = "手机号", required = true, dataType = "String")
    })
    public Response<Apply> isApply(String applicantPhone) {
        Response<Apply> jsonData=new Response<>();
//        JsonData jsonData = new JsonData();
        try {
            if (null == applicantPhone) {
                jsonData.setMessage("参数异常！");
            } else {
                Apply apply = new Apply();
                apply.setApplicantPhone(applicantPhone);
                Apply a = applyService.isApply(apply);

                jsonData.setResult(a);

                jsonData.setCode(0);
            }
        } catch (Exception ex) {
            logger.error("[ApplyController][isApply]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }
}
